﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using HigLabo.Net;
using HigLabo.WindowsAzure.RestService.Extensions;

namespace HigLabo.WindowsAzure.RestService.ServiceManagement
{
    public class ListSubscriptionOperationsCommand : ManagementServiceCommand<ListSubscriptionOperationsResult>
    {
        private DateTimeOffset StartOfTime;
        private DateTimeOffset EndOfTime;
        private String ObjectUrl;
        private String OpResultFilter;
        private String ContinuationToken;

        public ListSubscriptionOperationsCommand(DateTimeOffset startOfTime, DateTimeOffset endOfTime)
            : this(startOfTime, endOfTime, "", "", "")
        {

        }

        public ListSubscriptionOperationsCommand(DateTimeOffset startOfTime, DateTimeOffset endOfTime, String objectUrl)
            : this(startOfTime, endOfTime, objectUrl, "", "")
        {

        }

        public ListSubscriptionOperationsCommand(DateTimeOffset startOfTime, DateTimeOffset endOfTime, String objectUrl, String opResultFilter)
            : this(startOfTime, endOfTime, objectUrl, opResultFilter, "")
        {

        }

        public ListSubscriptionOperationsCommand(DateTimeOffset startOfTime, DateTimeOffset endOfTime, String objectUrl, String opResultFilter, String continuationToken)
        {
            DateTime date = DateTime.Now.AddDays(-90);
            TimeSpan t = startOfTime.Date - date.Date;
            if (t.Days < 0)
            {
                throw new ArgumentException("StartTime cannot indicate a start date of more than 90 days in the past.", "StartTime");
            }
            this.StartOfTime = startOfTime;
            this.EndOfTime = endOfTime;
            this.ObjectUrl = objectUrl;
            this.OpResultFilter = opResultFilter;
            this.ContinuationToken = continuationToken;
        }

        public override string GetRequestUri()
        {
            String requestUrl = String.Format("operations?StartTime={0}&EndTime={1}", StartOfTime.UtcDateTime.ToString("yyyy-MM-ddTHH:mm:ssZ")
                                                                                    , EndOfTime.UtcDateTime.ToString("yyyy-MM-ddTHH:mm:ssZ"));
            String optionalPar = "";
            if (!String.IsNullOrEmpty(ObjectUrl))
            {
                optionalPar = String.Format("&ObjectIdFilter={0}", ObjectUrl);
                if (!String.IsNullOrEmpty(OpResultFilter))
                {
                    optionalPar = String.Format(optionalPar + "&OperationResultFilter={0}", OpResultFilter);
                }
                if (!String.IsNullOrEmpty(ContinuationToken))
                {
                    optionalPar = String.Format(optionalPar + "&ContinuationToken={0}", ContinuationToken);
                }
            }
            return requestUrl + optionalPar;
        }

        public override HttpMethodName MethodName
        {
            get { return HttpMethodName.Get; }
        }

        public override Dictionary<String, String> GetRequestHeaders()
        {
            return null;
        }

        public override System.IO.Stream GetRequestBodyStream()
        {
            return null;
        }

    }
}
